package development;

import state.GameControl;
import state.GameForm;
import state.Graphics;
import state.TimerListener;
import state.font.FontManager;
import state.font.ImageFont;
import state.util.GameColor;

public class FrmDisplayNotificationMessage extends GameForm implements TimerListener {
  private int foregroundColor = GameColor.WHITE_CODE;
  private int showTime = 180000; // 3 min
  private String notificationMessage = "";
  private String lastMessage = "";
  
  private ImageFont font;
  private int currentX;
  private int startShowTime;
  
  private static GameForm singleton;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmDisplayNotificationMessage();
    }
    return singleton;
  }
  
  public void displayNotify(String notificationMessage1, int showTime1, int fontSize, int backgroundColor, int foregroundColor1) {
    if (lastMessage.equals(notificationMessage1)) {
      return;
    }
    this.notificationMessage = notificationMessage1;
    this.showTime = showTime1;
    this.foregroundColor = foregroundColor1;
    lastMessage = notificationMessage1;
    currentX = Constants.SCREEN_WIDTH;
    switch (fontSize) {
    case 8:
      font = FontManager.getFont(FontManager.FONT_SIZE_8);
      break;
    case 11:
      font = FontManager.getFont(FontManager.FONT_SIZE_11);
      break;
    case 13:
      font = FontManager.getFont(FontManager.FONT_SIZE_13);
      break;
    default:
      font = FontManager.getFont(FontManager.FONT_SIZE_8);
      break;
    }
    setSize(Constants.SCREEN_WIDTH, font.getHeight() + 3);
    _manager.setBackGroundColor(backgroundColor);
    new GameControl() {
      public void perform() {
        startShowTime = (int) System.currentTimeMillis();
        isRunning = true;
        Global.systemCanvas.masterForm = FrmDisplayNotificationMessage.this;
        Global.systemCanvas.isMasterFormHandleKey = false;
        setTimerListener(FrmDisplayNotificationMessage.this);
      }
    }.setTimeInterval(5000).start();
  }
  
  public void draw(Graphics g) {
    font.setBold(true);
    font.drawString2(g, notificationMessage, foregroundColor, currentX, 0, Constants.TOP_LEFT_ANCHOR);
    font.setBold(false);
  }

  public void doTask() {
    int strWidth = font.stringWidth(notificationMessage);
    if (currentX + strWidth > 10) {
      currentX -= 3;
    } else {
      currentX = Constants.SCREEN_WIDTH;
    }
    
    if (System.currentTimeMillis() - startShowTime > showTime) {
      removeTimerListener();
      Global.systemCanvas.masterForm = null;
      Global.systemCanvas.isMasterFormHandleKey = true;
      isRunning = false;
    }
  }
}
